Different variables are stored in memory in different ways. In Java, we can simply divide the data types of variables into two types:
As the name suggests, they are immutable (immutable) variables that have been defined (predfined) in Java. They are processed using the code-by-value method, which means that their values (stack memory addresses) are directly copied when passed in memory.
-> Includes: numbers (integers and decimals), characters char, Boolean boolean
Also known as Reference Types / Object type, because the way it stores values in memory is by reference (heap memory address). It is not defined in advance in Java and needs to be created by the programmer.
-> Includes: String*, Array*, Classes*, etc.
Now we know that a String is a class in Java that is made up of a sequence of characters. Then provide some practical keywords to make the use of strings more flexible!!!
String code = "Hello World!";
System.out.println( code.length() ); //Output : 12
Program analysis: When calculating the length of a string, spaces and punctuation are included. And remember to add a period (.) before a keyword when you want to use a command.
String greeting = "Hello ";
String person = "Chi";
String NewG = greeting.toUpperCase();
String NewP = person.toLowerCase();
System.out.println(NewG.concat(NewP)); //Outputs : HELLO chi
String txt = "Please locate where 'locate' occurs!";
System.out.println( txt.indexOf("locate") ); // Outputs 7 (內容注意使用雙引號)
String NewG = greeting.toUppercase();
這裡的toUppercase()應該改成 toUpperCase(),大小寫寫錯跑起來會有error發生
謝謝您的指教,已經更正!